home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Risc World 3
/
Risc World 3.iso
/
SOFTWARE
/
ISSUE6
/
PD
/
PDF
/
Stl
/
h
/
string
< prev
Wrap
Text File
|
2003-02-14
|
14KB
|
343 lines
//--------------------------------------------------------------------------
//
// Copyright (c) 2002, Colin Granville
//
// All rights reserved.
//
// Redistribution and use in source and binary forms, with or
// without modification, are permitted provided that the following
// conditions are met:
//
// * Redistributions of source code must retain the above copyright
// notice, this list of conditions and the following disclaimer.
//
// * Redistributions in binary form must reproduce the above
// copyright notice, this list of conditions and the following
// disclaimer in the documentation and/or other materials
// provided with the distribution.
//
// * The name Colin Granville may not be used to endorse or promote
// products derived from this software without specific prior
// written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
// FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
// COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
// INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
// (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
// SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
// HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
// STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
// ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
// OF THE POSSIBILITY OF SUCH DAMAGE.
//
//--------------------------------------------------------------------------
#ifndef stl_string_h
#define stl_string_h
#include "bool.h"
#include <string.h>
class istream;
class ostream;
/* NOTE
******** compiler will not create functions inline unless declared prior to use *******
*/
class string;
string operator+(const string& lhs, const string& rhs);
string operator+(const char* lhs, const string& rhs);
string operator+(char lhs, const string& rhs);
string operator+(const string& lhs, const char* rhs);
string operator+(const string& lhs, char rhs);
bool operator==(const string& lhs, const string& rhs);
bool operator==(const char* lhs, const string& rhs);
bool operator==(const string& lhs, const char* rhs);
bool operator!=(const string& lhs, const string& rhs);
bool operator!=(const char* lhs, const string& rhs);
bool operator!=(const string& lhs, const char* rhs);
bool operator< (const string& lhs, const string& rhs);
bool operator< (const string& lhs, const char* rhs);
bool operator< (const char* lhs, const string& rhs);
bool operator> (const string& lhs, const string& rhs);
bool operator> (const string& lhs, const char* rhs);
bool operator> (const char* lhs, const string& rhs);
bool operator<=(const string& lhs, const string& rhs);
bool operator<=(const string& lhs, const char* rhs);
bool operator<=(const char* lhs, const string& rhs);
bool operator>=(const string& lhs, const string& rhs);
bool operator>=(const string& lhs, const char* rhs);
bool operator>=(const char* lhs, const string& rhs);
void swap(string& lhs, string& rhs);
istream& operator>>(istream& is, string& str);
ostream& operator<<(ostream& os, const string& str);
istream& getline(istream& is, string& str, char delim='\n');
class string
{
public:
typedef unsigned int size_type;
typedef char* iterator;
typedef const char* const_iterator;
enum {npos=-1}; // *largest* size_type
// construct/copy/destroy
string();
string(const string& str,size_type pos=0,size_type n=npos);
string(const char* s,size_type n);
string(const char* s);
string(size_type n,char c);
string(iterator begin,iterator end);
~string();
string& operator=(const string& str);
string& operator=(const char* s);
string& operator=(char c);
// iterators
iterator begin();
const_iterator begin() const;
iterator end();
const_iterator end() const;
iterator rbegin();
const_iterator rbegin() const;
iterator rend();
const_iterator rend() const;
// capacity
size_type size() const;
size_type length() const;
size_type max_size() const;
void resize(size_type n, char c);
void resize(size_type n);
size_type capacity() const;
void reserve(size_type res_arg = 0);
void clear();
bool empty() const;
// element access:
const char& operator[](size_type pos) const;
char& operator[](size_type pos);
const char& at(size_type n) const;
char& at(size_type n);
// _lib.string.modifiers_ modifiers:
string& operator+=(const string& str);
string& operator+=(const char* s);
string& operator+=(char c);
string& append(const string& str);
string& append(const string& str, size_type pos,size_type n=npos);
string& append(const char* s, size_type n);
string& append(const char* s);
string& append(size_type n, char c);
string& append(iterator first, iterator last);
string& assign(const string&);
string& assign(const string& str, size_type pos,size_type n=npos);
string& assign(const char* s, size_type n);
string& assign(const char* s);
string& assign(size_type n, char c);
string& assign(iterator first, iterator last);
string& insert(size_type pos1, const string& str);
string& insert(size_type pos1, const string& str,size_type pos2, size_type n=npos);
string& insert(size_type pos, const char* s, size_type n);
string& insert(size_type pos, const char* s);
string& insert(size_type pos, size_type n, char c);
iterator insert(iterator i, char c = 0);
void insert(iterator i, size_type n, char c);
void insert(iterator i, iterator first, iterator last);
string& erase(size_type pos = 0, size_type n = npos);
iterator erase(iterator position);
iterator erase(iterator first, iterator last);
string& replace(size_type pos1, size_type n1, const string& str);
string& replace(size_type pos1, size_type n1, const string& str,
size_type pos2, size_type n2);
string& replace(size_type pos, size_type n1, const char* s,size_type n2);
string& replace(size_type pos, size_type n1, const char* s);
string& replace(size_type pos, size_type n1, size_type n2, char c);
string& replace(iterator i1, iterator i2, const string& str);
string& replace(iterator i1, iterator i2, const char* s, size_type n);
string& replace(iterator i1, iterator i2, const char* s);
string& replace(iterator i1, iterator i2, size_type n, char c);
string& replace(iterator i1, iterator i2, iterator j1, iterator j2);
size_type copy(char* s, size_type n, size_type pos = 0) const;
void swap(string&);
// string operations:
const char* c_str() const; // explicit
const char* data() const;
// allocator_type get_allocator() const;
size_type find (const string& str, size_type pos = 0) const;
size_type find (const char* s, size_type pos, size_type n) const;
size_type find (const char* s, size_type pos = 0) const;
size_type find (char c, size_type pos = 0) const;
size_type rfind(const string& str, size_type pos = npos) const;
size_type rfind(const char* s, size_type pos, size_type n) const;
size_type rfind(const char* s, size_type pos = npos) const;
size_type rfind(char c, size_type pos = npos) const;
size_type find_first_of(const string& str, size_type pos = 0) const;
size_type find_first_of(const char* s, size_type pos, size_type n) const;
size_type find_first_of(const char* s, size_type pos = 0) const;
size_type find_first_of(char c, size_type pos = 0) const;
size_type find_last_of (const string& str,size_type pos = npos) const;
size_type find_last_of (const char* s, size_type pos, size_type n) const;
size_type find_last_of (const char* s, size_type pos = npos) const;
size_type find_last_of (char c, size_type pos = npos) const;
size_type find_first_not_of(const string& str, size_type pos = 0) const;
size_type find_first_not_of(const char* s, size_type pos, size_type n) const;
size_type find_first_not_of(const char* s, size_type pos = 0) const;
size_type find_first_not_of(char c, size_type pos = 0) const;
size_type find_last_not_of (const string& str, size_type pos = npos) const;
size_type find_last_not_of (const char* s, size_type pos, size_type n) const;
size_type find_last_not_of (const char* s, size_type pos = npos) const;
size_type find_last_not_of (char c, size_type pos = npos) const;
string substr(size_type pos = 0, size_type n = npos) const;
int compare(const string& str) const;
int compare(size_type pos1, size_type n1, const string& str) const;
int compare(size_type pos1, size_type n1, const string& str,
size_type pos2, size_type n2) const;
int compare(const char* s) const;
int compare(size_type pos1, size_type n1, const char* s, size_type n2 = npos) const;
static string add(const char *s1,int len1,const char *s2,int len2);
private:
struct InternalString
{
struct Hdr
{
size_type size;
size_type capacity;
size_type count;
Hdr() :
size(0),
capacity(0),
count(2) {}
}hdr;
char data[1];
InternalString() {data[0]=0;}
} *p;
static InternalString null_string;
InternalString* allocate(size_type n);
void deallocate(InternalString*);
bool reserve_now(size_type n);
bool midextend(size_type pos,int by);
};
inline string::size_type string::size() const {return p->hdr.size;}
inline string::size_type string::length() const {return size();}
inline string::size_type string::capacity() const {return p->hdr.capacity;}
inline string::size_type string::max_size() const {return 512;}
inline void string::resize(size_type n) {resize(n,' ');}
inline void string::clear() {deallocate(p);p=&null_string;}
inline bool string::empty() const {return size()==0;}
inline const char* string::c_str() const {p->data[size()]=0;return p->data;}
inline const char* string::data() const {return p->data;}
inline string& string::operator=(const string& str) {return assign(str);}
inline string& string::operator=(const char* s) {return assign(s);}
inline string& string::operator=(char c) {return assign(1,c);}
inline string::const_iterator string::begin() const {return p->data;}
inline string::iterator string::begin() {reserve_now(size());return p->data;}
inline string::const_iterator string::end() const {return begin()+size();}
inline string::iterator string::end() {return begin()+size();}
inline string::const_iterator string::rbegin() const {return begin()+size();}
inline string::iterator string::rbegin() {return begin()+size();}
inline string::const_iterator string::rend() const {return begin();}
inline string::iterator string::rend() {return begin();}
inline const char& string::at(size_type n) const {return *(begin()+n);}
inline char& string::at(size_type n) {return *(begin()+n);}
inline const char& string::operator[](size_type pos) const {return at(pos);}
inline char& string::operator[](size_type pos) {return at(pos);}
inline string& string::operator+=(const string& str) {return append(str);}
inline string& string::operator+=(const char* s) {return append(s);}
inline string& string::operator+=(char c) {return append(1,c);}
inline string& string::assign(iterator first, iterator last)
{return assign(first,last-first);}
inline void string::swap(string& str) {InternalString *is=p;p=str.p;str.p=is;}
inline string string::substr(size_type pos, size_type n) const
{return string(*this,pos,n);}
//******************************************************************
inline string operator+(const string& lhs, const string& rhs)
{return string::add(lhs.data(),lhs.size(),rhs.data(),rhs.size());}
inline string operator+(const char* lhs, const string& rhs)
{return string::add(lhs,strlen(lhs),rhs.data(),rhs.size());}
inline string operator+(const string& lhs, const char* rhs)
{return string::add(lhs.data(),lhs.size(),rhs,strlen(rhs));}
inline bool operator==(const string& lhs, const string& rhs) {return lhs.compare(rhs)==0;}
inline bool operator==(const char* lhs, const string& rhs) {return rhs.compare(lhs)==0;}
inline bool operator==(const string& lhs, const char* rhs) {return lhs.compare(rhs)==0;}
inline bool operator!=(const string& lhs, const string& rhs) {return lhs.compare(rhs)!=0;}
inline bool operator!=(const char* lhs, const string& rhs) {return rhs.compare(lhs)!=0;}
inline bool operator!=(const string& lhs, const char* rhs) {return lhs.compare(rhs)!=0;}
inline bool operator< (const string& lhs, const string& rhs) {return lhs.compare(rhs)<0;}
inline bool operator< (const string& lhs, const char* rhs) {return lhs.compare(rhs)<0;}
inline bool operator< (const char* lhs, const string& rhs) {return rhs.compare(lhs)>=0;}
inline bool operator> (const string& lhs, const string& rhs) {return lhs.compare(rhs)>0;}
inline bool operator> (const string& lhs, const char* rhs) {return lhs.compare(rhs)>0;}
inline bool operator> (const char* lhs, const string& rhs) {return rhs.compare(lhs)<=0;}
inline bool operator<=(const string& lhs, const string& rhs) {return lhs.compare(rhs)<=0;}
inline bool operator<=(const string& lhs, const char* rhs) {return lhs.compare(rhs)<=0;}
inline bool operator<=(const char* lhs, const string& rhs) {return rhs.compare(lhs)>0;}
inline bool operator>=(const string& lhs, const string& rhs) {return lhs.compare(rhs)>=0;}
inline bool operator>=(const string& lhs, const char* rhs) {return lhs.compare(rhs)>=0;}
inline bool operator>=(const char* lhs, const string& rhs) {return rhs.compare(lhs)<0;}
inline void swap(string& lhs, string& rhs) {lhs.swap(rhs);}
#endif